﻿@{
    var web = WebSiteContext.Current.Web;
    Layout = "~/Views/Shared/_Dashboard.cshtml";
}
@UIHelper.ImageTitle(Html.Global("RES_PAGEMAN"), Html.Global("RES_PAGEMAN_DESC"), "~/content/images/icon_page_process_48.png")
<input name="parentID" type="hidden" id="parentID" value="0" />
<table cellpadding="0" cellspacing="2" style="width: 100%;">
    <tr>
        <td colspan="2">
            @{
                Ajax.Dna().Toolbar()
                    .Name("PageManToolbar")
                    .Buttons(btns =>
                    {
                        btns.AddButton(Html.Global("RES_CREATEPAGE"), "").ImageIcons(Url.Content("~/content/images/add_page.png")).Click("location=\"" + Url.Action("Create", "DynamicUI", new { Area = "", returnUrl = Url.Action("PageManager", "DynamicUI") }) + "\"");
                        btns.AddButton(Html.Global("SEO_Report"), "location=\"" + Url.Action("SEOSuggestions", "DynamicUI", new { Area = "", website = web.Name }) + "\"");
                        btns.AddSpliter();
                        btns.AddButton(Html.Global("RES_HELP"), "window.open(\"" + Url.Help(1) + "\");").ImageIcons(Url.Content("~/content/images/icon_help_16.png"));
                    })
                    .Render();
            }
        </td>
    </tr>
    <tr>
        <td style="width: 300px; vertical-align: top;">
            @{
                string defaultUrl = web.DefaultUrl;

                Ajax.Dna().TreeView("siteMapTree")
                             .HtmlAttributes(new { style = "padding:5px;overflow:auto;margin:0px;" })
                             .ExpandedLevel(1)
                            .Options(opts =>
                            {
                                //opts.OnNodeSelected = "$('#parentID').val($(node).attr('key')); $('#parentID').parents('form').submit();";
                                opts.OnNodeSelected = "_getSubPages($(node).data('key'));";
                                opts.OnNodeDropped = "$.post('" + Url.Action("Move", "DynamicUI", new { Area = "" }) + "', { id: ui.node.data('key'), pos: ui.position,parentID:ui.targetNode.data('key') },function(){ $('#parentID').val(ui.targetNode.data('key'));$('#parentID').parents('form').submit();});";
                            })
                            .Bind(WebSiteContext.Current.SiteMapNodeProvider.RootNode, (node) =>
                            {
                                if (node.Key.Equals("0"))
                                    node.ImageUrl = Url.Content("~/content/images/icon_earth_16.gif");
                                else
                                    node.ImageUrl = Url.Content("~/content/images/icon_aspx_16.png");

                                bool showInMenu = node.Attributes.ContainsKey("data-visible") ? bool.Parse(node.Attributes["data-visible"].ToString().ToLower()) : true;
                                var page = node.Item as DNA.Mvc.WebPage;
                                if (!showInMenu)
                                {
                                    node.Attributes.Add("style", "color: #cccccc;");
                                    node.ImageUrl = Url.Content("~/content/images/icon_hide_aspx_16.png");
                                }
                                else
                                {
                                    if (node.Attributes.ContainsKey("style"))
                                        node.Attributes["style"] = "color:#000000;";
                                    else
                                        node.Attributes.Add("style", "color:#000000;");
                                }

                                if ((page != null) && (!page.AllowAnonymous))
                                    node.ImageUrl = Url.Content("~/content/images/icon_security_aspx_16.png");

                                if ((page != null) && (!string.IsNullOrEmpty(defaultUrl)))
                                {
                                    if (defaultUrl.Equals(page.Path, StringComparison.OrdinalIgnoreCase))
                                        node.ImageUrl = Url.Content("~/content/images/icon_home_16.png");
                                }
                                
                                node.NavigateUrl = "";
                                return true;
                            })
                            .DragAndDrop(true)
                            .Render();
            }
        </td>
        <td valign="top">
            <div id="pageListContainer">
                @{ Html.RenderAction("SubPages", "DynamicUI", new { parentID = 0 });}
            </div>
        </td>
    </tr>
</table>
<script type="text/javascript">
    function _getSubPages(id) {
        $.ajax({
            type: "GET",
            url: "@Url.Action("SubPages", "DynamicUI")",
            data: { parentID: id },
            success: function (dat) {
                $("#pageListContainer").html(dat);
            }
        });
    }
</script>
@Html.StartupScripts()
